package com.triptogether.shoppinglist.activities;

import android.os.Bundle;
import android.view.MenuItem;

import com.triptogether.baseclasses.BaseActivity;
import com.triptogether.shoppinglist.fragments.ProductsListFragment;
import com.triptogether.shoppinglist.fragments.ShoppingListMainFragment;
import com.triptogether.utils.db.DBManager;
import com.triptogether.utils.db.models.ShoppingList;

public class OnePaneProductsActivity extends BaseActivity {
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		// Set the home button in the ActionBar as displayed
		setDisplayHomeAsUpEnabled(true);
		
		// Get the chosen products list from the intent and init a Products list fragment
		long shoppingListId = getIntent().getLongExtra(ProductsListFragment.ARG_SHOPPING_LIST_ID, Long.MIN_VALUE);
    	ProductsListFragment initiatedProductsListFragment = ShoppingListMainFragment.initProductListFragment(shoppingListId);
    	
    	// Set the initialized Products List as the content
    	getSupportFragmentManager().beginTransaction().replace(android.R.id.content, initiatedProductsListFragment).commit();
    	
    	// Set title for the activity
		ShoppingList findShoppingListById = DBManager.getInstance(this).findShoppingListById(shoppingListId);
		setTitle(findShoppingListById.getShoppingListName());
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case android.R.id.home:
				finish();
				overridePendingTransition(0, 0);
				return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onBackPressed() {
	    super.onBackPressed();

		overridePendingTransition(0, 0);
	}
}